home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0063_Extracting Index Data from a Table.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  6.8 KB  |  190 lines

  1. {
  2.  
  3.  
  4. The IndexDefs property of the TTable component contains information about
  5. the indexes for the table used by the TTable. The IndexDefs property
  6. itself has various properties that allow for the extraction of information
  7. about specific indexes. The two properties in the IndexDefs object are:
  8.  
  9.   Count: type Integer; available only at run-time and read-only; indicates
  10.          the number ofentries in the Items property (i.e., the number of
  11.          indexes in the table).
  12.   Items: type TIndexDef; available only at run-time and read-only; an
  13.          array of TIndexDef objects, one for each index in the table.
  14.          
  15. The Count property of the IndexDefs object is used as the basis for a
  16. loop program construct to iterate through the Items property entries to
  17. extract specific information about each index. Each IndexDef object con-
  18. tained in the Items property consists of a number of properties that pro-
  19. vide various bits of information that describe each index. All of the
  20. properties of the IndexDef object are available only at run-time and are
  21. all read-only. These properties are:
  22.  
  23.   Expression: type String; indicates the expression used for dBASE multi-
  24.               field indexes.
  25.   Fields:     type String; indicates the field or fields upon which the
  26.               index is based.
  27.   Name:       type String; name of the index.
  28.   Options:    type TIndexOptions; characteristics of the index (ixPrimary,
  29.               ixUnique, etc.).
  30.  
  31. Before any index information (Count or Items) can be accessed, the Update
  32. method of the IndexDefs object must be called. This refreshes or init-
  33. ializes the IndexDef object's view of the set of indexes.
  34.  
  35. Examples
  36. ========
  37.  
  38. Here is a simple For loop based on the Count property of the IndexDefs
  39. object that extracts the name of each index (if any exist) for the table
  40. represented by the TTable component Table1:
  41. }
  42.  
  43.   procedure TForm1.ListBtnClick(Sender: TObject);
  44.   var
  45.     i: Integer;
  46.   begin
  47.     ListBox1.Items.Clear;
  48.     with Table1 do begin
  49.       if IndexDefs.Count > 0 then begin
  50.         for i := 0 to IndexDefs.Count - 1 do
  51.           ListBox1.Items.Add(IndexDefs.Items[i].Name)
  52.       end;
  53.     end;
  54.   end;
  55.  
  56. Below is an example showing how to extract information about indexes at
  57. run-time, plugging the extracted values into a TStringGrid (named SG1).
  58.  
  59.   procedure TForm1.FormShow(Sender: TObject);
  60.   var
  61.     i: Integer;
  62.     S: String;
  63.   begin
  64.     with Table1 do begin
  65.       Open;
  66.       {Refresh IndexDefs object}
  67.       IndexDefs.Update;
  68.       if IndexDefs.Count > 0 then begin
  69.         {Set up columns and rows in grid to match IndexDefs items}
  70.         SG1.ColCount := 4;
  71.         SG1.RowCount := IndexDefs.Count + 1;
  72.         {Set grid column labels to TIndexDef property names}
  73.         SG1.Cells[0, 0] := 'Name';
  74.         SG1.ColWidths[0] := 200;
  75.         SG1.Cells[1, 0] := 'Fields';
  76.         SG1.ColWidths[1] := 200;
  77.         SG1.Cells[2, 0] := 'Expression';
  78.         SG1.ColWidths[2] := 200;
  79.         SG1.Cells[3, 0] := 'Options';
  80.         SG1.ColWidths[3] := 300;
  81.  
  82.         {Loop through IndexDefs.Items}
  83.         for i := 0 to IndexDefs.Count - 1 do begin
  84.           {Fill grid cells for current row}
  85.           SG1.Cells[0, i + 1] := IndexDefs.Items[i].Name;
  86.           SG1.Cells[1, i + 1] := IndexDefs.Items[i].Fields;
  87.           SG1.Cells[2, i + 1] := IndexDefs.Items[i].Expression;
  88.           if ixPrimary in IndexDefs.Items[i].Options then
  89.             S := 'ixPrimary, ';
  90.           if ixUnique in IndexDefs.Items[i].Options then
  91.             S := S + 'ixUnique, ';
  92.           if ixDescending in IndexDefs.Items[i].Options then
  93.             S := S + 'ixDescending, ';
  94.           if ixCaseInsensitive in IndexDefs.Items[i].Options then
  95.             S := S + 'ixCaseInsensitive, ';
  96.           if ixExpression in IndexDefs.Items[i].Options then
  97.             S := S + 'ixExpression, ';
  98.           if S > ' ' then begin
  99.             {Get rid of trailing ", "}
  100.             System.Delete(S, Length(S) - 1, 2);
  101.             SG1.Cells[3, i + 1] := S;
  102.           end;
  103.         end;
  104.       end;
  105.     end;
  106.   end;
  107.  
  108. Special Considerations
  109. ======================
  110.  
  111. There are idiosyncracies associated with extracting information about
  112. indexes for different table types that Delphi can access.
  113.  
  114. dBASE Tables
  115. ------------
  116.  
  117. With dBASE indexes, which properties of Fields and Expression will be
  118. filled will depend on the type of index, simple (single-field) or
  119. complex (based on multiple fields or a dBASE expression). If the index
  120. is a simple one, the Fields property will contain the name of the field
  121. in the table on which the index is based and the Expression property will
  122. be blank. If the index is a complex one, the Expression property will
  123. show the expression on which the index is based (e.g., "Field1+Field2")
  124. and the Fields property will be blank.
  125.  
  126. Paradox Tables
  127. --------------
  128.  
  129. With Paradox primary indexes, the Name property will be blank, the Fields
  130. property will contain the field(s) on which the index is based, and the
  131. Options property will contain ixPrimary. With secondary indexes, the Name
  132. property will contain the name of the secondary index, the Fields prop-
  133. erty will contain the field(s) on which the index is based, and the
  134. Options property may or may not have values.
  135.  
  136. The Fields property for indexes based on more than one field will show
  137. the field names separated by semi-colons. Indexes based on only a single
  138. field will show the name of only that one field in the Fields property.
  139.  
  140. InterBase Tables
  141. ----------------
  142.  
  143. For both index types, single- or multiple-field, the Expression property
  144. will be blank. For single-field indexes, the Fields property will contain
  145. the field on which the index is based. For multi-field indexes, the Fields
  146. property will show all of the multiple fields that comprise the index,
  147. each separated by a semi-colon. 
  148.  
  149. Indexes designated as PRIMARY when the CREATE TABLE command is issued will
  150. have "RDB$PRIMARYn" in the Name property, where n is a number character
  151. uniquely identifying the primary index within the database metadata.
  152. Secondary indexes will show the actual name of the index.
  153.  
  154. Foreign key constraints also result in an index being created by the sys-
  155. tem. These indexes appear in the IndexDefs property, and will have the
  156. name "RDB$FOREIGNn" where n is a number character that uniquely identifies
  157. the index within the database metadata.
  158.  
  159. The Fields property for indexes based on more than one field will show
  160. the field names separated by semi-colons. Indexes based on only a single
  161. field will show the name of only that one field in the Fields property.
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. DISCLAIMER: You have the right to use this technical information
  187. subject to the terms of the No-Nonsense License Statement that
  188. you received with the Borland product to which this information
  189. pertains.
  190.